Imports ADODB
Imports ADODB.DataTypeEnum

Namespace Result
    Public MustInherit Class RsWriter
        Public ReadOnly Source As Recordset
        Public ReadOnly Target As String = ""
        Public FieldFormats As New Dictionary(Of String, String)

        Public Function FormattedValueString(ByVal Field As ADODB.Field) As String
            If Field.Value Is Nothing Then Return ""
            If Field.Value.Equals(System.DBNull.Value) Then Return ""
            If FieldFormats(Field.Name).Length = 0 Then
                Return Field.Value.ToString
            Else
                Return Strings.Format(Field.Value, FieldFormats(Field.Name))
            End If
        End Function

        Public Sub New(ByVal Source As ADODB.Recordset, ByVal Target As String)
            Me.Source = Source
            Me.Target = Target
            For Each f As Field In Source.Fields
                Select Case f.Type
                    Case adDBDate, adDate
                        FieldFormats(f.Name) = "yyyy-MM-dd"
                    Case adDBTime
                        FieldFormats(f.Name) = "hh:mm:ss"
                    Case adDecimal, adCurrency, adDouble, adNumeric, adSingle
                        FieldFormats(f.Name) = "#0.00"
                    Case adBigInt, adInteger, adSmallInt, adTinyInt
                        FieldFormats(f.Name) = "0"
                    Case Else
                        FieldFormats(f.Name) = ""
                End Select
            Next
        End Sub

        Public MustOverride Function Execute() As Long

    End Class
End Namespace
